import Vue from "vue";

export const messageInfo = function (message, options) {
  if (message) {
    Vue.prototype.$message({
      message,
      type: "info",
      ...options,
    });
  }
};

export const messageSuccess = function (message, options) {
  if (message) {
    Vue.prototype.$message({
      message,
      type: "success",
      ...options,
    });
  }
};

export const messageWarning = function (message, options) {
  if (message) {
    Vue.prototype.$message({
      message,
      type: "warning",
      ...options,
    });
  }
};

export const messageError = function (message, options) {
  if (message) {
    Vue.prototype.$message({
      message,
      duration: 5000,
      type: "error",
      ...options,
    });
  }
};

export const notifyInfo = function (title, message, options) {
  Vue.prototype.$notify({
    title,
    message,
    type: "info",
    ...options,
  });
};

export const notifySuccess = function (title, message, options) {
  Vue.prototype.$notify({
    title,
    message,
    type: "success",
    ...options,
  });
};

export const notifyWarning = function (title, message, options) {
  Vue.prototype.$notify({
    title,
    message,
    type: "warning",
    ...options,
  });
};

export const notifyError = function (title, message, options) {
  Vue.prototype.$notify({
    title,
    message,
    type: "error",
    ...options,
  });
};

export const messageWarningConfirm = function (
  message,
  title = "提示",
  options = {
    confirmButtonText: "确 定",
    cancelButtonText: "取 消",
  },
) {
  return Vue.prototype.$confirm(
    message,
    title,
    (options = {
      type: "warning",
      ...options,
    }),
  );
};

export const handleError = (rules, form) => {
  const keys = Object.keys(rules);
  for (let i = 0; i < keys.length; i++) {
    const record = keys[i];
    const rule = rules[record][0];
    if (rule && rule.required) {
      if (form[record] === "" || form[record] === null || form[record] === undefined) {
        Vue.prototype.$message.error(rule.message);
        return false;
      }
    }
  }
  return true;
};
